﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Windows.Input;
using ShredTheLog.Api;
using System.ComponentModel.Composition;
using ShredTheLog.Api.DataContracts;
using ShredTheLog.Common.MVVM;
using System.Diagnostics.Contracts;

namespace ShredTheLog.ViewModels
{
    [Export(typeof(ISettingsPageViewModel))]
    [OrderMetadata(Order = 0)]
    [PartCreationPolicy(CreationPolicy.NonShared)] // Модель должна пересоздаваться, чтобы после закрытия окна настроек по отмене окно настроек при повторном открытии отображало актуальные значения настроек, а не отмененные в ходе предыдущего сеанса
    public sealed class UiSettingsPageViewModel : ViewModelBase, ISettingsPageViewModel
    {
        [Import]
        private IShredServices ShredServices { get; set; }

        [Import]
        private IUiSettingsSection UiSettings { get; set; }

        [ImportMany]
        private Lazy<ITheme, IOrderMetadata>[] _themes = null;
        public IEnumerable<ITheme> Themes
        {
            get 
            {
                Contract.Assume(_themes != null);
                return from i in _themes orderby i.Metadata.Order select i.Value; 
            }
        }

        [ImportMany]
        private Lazy<IUiLanguage, IOrderMetadata>[] _uiLanguages = null;
        public IEnumerable<IUiLanguage> UiLanguages
        {
            get 
            {
                Contract.Assume(_uiLanguages != null);
                return from i in _uiLanguages orderby i.Metadata.Order select i.Value; 
            }
        }

        public UiSettingsPageViewModel()
        {
            ThemeID = UiSettings.ThemeID;
            UiLanguage = UiSettings.UiLanguage;
            SendMessageGesture = UiSettings.SendMessageGesture;
            SendTypingNotifications = UiSettings.SendTypingNotifications;
            ShowMessageTime = UiSettings.ShowMessageTime;
        }

        public string Header { get { return ShredServices.GetLocalizedString("s_UserInterface"); } }
        public string ThemeID { get; set; }
        public Language UiLanguage { get; set; }
        public KeyGesture SendMessageGesture { get; set; }
        public bool SendTypingNotifications { get; set; }
        public bool ShowMessageTime { get; set; }

        public void Save()
        {
            UiSettings.ThemeID = ThemeID;
            UiSettings.UiLanguage = UiLanguage;
            UiSettings.SendMessageGesture = SendMessageGesture;
            UiSettings.SendTypingNotifications = SendTypingNotifications;
            UiSettings.ShowMessageTime = ShowMessageTime;
        }
    }
}
